<?php
if (!defined('admin')) {
    exit();
}

if (power('alevel') != 3) {
    echo resp_error_json('权限不足');
    exit();
}


$id = getGet('id');
$ifshowStr = '';
if (isset($_GET['ifshow'])) {
    $ifshow = $_GET['ifshow'] ? 1 : 0;
    $ifshowStr = "ifshow='$ifshow'";
}

$morderStr = "";
if (isset($_GET['morder'])) {
    $morder = $_GET['morder'];
    if (is_numeric($morder)) {
        if ($ifshowStr) {
            $morderStr = ",morder=$morder";
        } else {
            $morderStr = "morder=$morder";
        }
    } else {
        echo resp_error_json('参数错误');
        exit();
    }
}

$msettingStr = '';
if (isset($_GET['adminsearch'])) {
    $query = M()->query("SELECT msetting FROM " . tableex('moudle') . " where id='$id'");
    $link = M()->fetchOne($query);
    $msetting = json_decode($link['msetting'], 1);
    if (getGet('adminsearch')) {
        $msetting['adminsearch'] = 'on';
    } else {
        unset($msetting['adminsearch']);
    }
    if (!empty($ifshowStr) || !empty($morderStr)) {
        $msettingStr = ",msetting='" . dbstr(json_encode($msetting)) . "'";
    } else {
        $msettingStr = "msetting='" . dbstr(json_encode($msetting)) . "'";
    }
}

if (empty($ifshowStr) && empty($msettingStr) && empty($morderStr)) {
    exit(resp_error_json('无效参数'));
}

$sql="UPDATE " . tableex('moudle') . " SET {$ifshowStr} {$morderStr} {$msettingStr} WHERE id='$id'";
//dump_pre($sql);
$query = M()->query($sql);

if ($query) {
    echo resp_success_json('1');
} else {
    echo resp_error_json('修改失败，请刷新页面再试');
}
